<?php
/* 
 * withdrawcontroller definition
 * @author li.junfu
 */
require_once BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once BaseUtil::getModulePath('dbgrid') . '/DBGrid.php';
require_once BaseUtil::getModulePath("trade")  .  '/services/WithDrawService.php';
require_once BaseUtil::getModulePath("trade")  .  '/constants/TradeConstant.php';
require_once BaseUtil::getModulePath("trade")  .  '/forms/OffLineAddWithDrawForm.php';
require_once BaseUtil::getModulePath("trade")  .  '/forms/SureWithDrawOffLineForm.php';
require_once BaseUtil::getModulePath("trade")  .  '/forms/SureWithDrawLineForm.php';

class Trade_WithdrawController extends BaseController {
    
    public function init(){
        parent::init();
        $this->view->script = array('lib/ui.dialog.js','withdraw.js');
    }

    public function searchlineAction(){
        $dbgrid = new DBGrid($this->view);
        $dbgrid->setPost($_POST);
        $dbgrid->setSortOrder("account_agent_trade_logs-created");
        //$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-sn", $this->_translate->translate("SN"));
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-trade_num", $this->_translate->translate("tradeNum"));
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "users-name", $this->_translate->translate("userId"));
        $dbgrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-status", $this->_translate->translate("status"),TradeConstant::getWithdrawStatusList());
        $dbgrid->addDateFilter("account_agent_trade_logs-created", $this->_translate->translate("withdrawApplyDate"));
        $dbgrid->addValueFilter('account_agent_trade_logs-trade_type', '3');
        $dbgrid->addValueFilter('account_agent_trade_logs-withdraw_type', array("1", "2", "3"), DBGridConstant::OPERATOR_IN);
        $dbgrid->addValueFilter('account_agent_trade_logs-status', array("4", "5","7"), DBGridConstant::OPERATOR_IN);
        $dbgrid->addValueFilter('account_agent_trade_logs-is_delete', 0);
        $dbgrid->addFetchTable("account_agent_trade_logs", "*");
        $dbgrid->addFetchTable("users", array("name"),"users.id = account_agent_trade_logs.user_id and users.is_delete = 0");        
        //$dbgrid->addFetchColumn("sn", $this->_translate->translate("SN"));
        $dbgrid->addFetchColumn("trade_num", $this->_translate->translate("tradeNum"),"online");
        $dbgrid->addFetchColumn("name", $this->_translate->translate("userId"));
        $dbgrid->addFetchColumn("in_id", $this->_translate->translate("userAccount2"));
        $dbgrid->addFetchColumn("trade_sum", $this->_translate->translate("tradeSum"));
        $dbgrid->addFetchColumn("withdraw_type", $this->_translate->translate("withDrawType2"), "", TradeConstant::getTypeList());
        $dbgrid->addFetchColumn("status", $this->_translate->translate("status2"), "", TradeConstant::getStatusList());
        $dbgrid->addFetchColumn("created", $this->_translate->translate("withdrawApplyDate"));
        //$dbgrid->addFetchColumn("in_date", $this->_translate->translate("inDate"));
        $dbgrid->setPageTitle($this->_translate->translate("withDraw"));
        $dbgrid->setModule('/trade/withdraw');
//        if(AAService::isAllowed("withdraw", "online")) {
//            $dbgrid->addColAction("online", $this->_translate->translate("lookInfor"), "transaction.gif");
//        }
//        if(AAService::isAllowed("withdraw", "deleteline")) {
//            $dbgrid->addColAction("deleteline", $this->_translate->translate("delBtn"), "delete.gif");
//        }
        $this->view->dbGrid = $dbgrid->render();
    }

    //withdrawonline log
    public function onlineAction(){
        $id = $this->_getParam('id');
        $withDrawService = new WithDrawService();
        $withDraw = $withDrawService->getWithDrawInfoById($id);
        $this->view->withdraw = $withDraw;
        $this->view->id = $id;
        if($withDraw['status'] == 4){
            $this->view->form = new SureWithDrawLineForm();
            return $this->render('online');      //
        }
        if($withDraw['status'] == 5){
            $this->view->withdraw=$withDraw;
            return $this->render('onlinealreadyin');   //
        }
        if($withDraw['status'] == 7){
            $this->view->withdraw=$withDraw;
            return $this->render('onlinereject');      //
        }
    }

    public function searchlinewaitAction(){
        $dbgrid = new DBGrid($this->view);
        $dbgrid->setPost($_POST);
        $dbgrid->setSortOrder("account_agent_trade_logs-created");
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-sn", $this->_translate->translate("SN"));
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "users-name", $this->_translate->translate("userId"));
        $dbgrid->addDateFilter("account_agent_trade_logs-created", $this->_translate->translate("withdrawApplyDate"));
        $dbgrid->addValueFilter('account_agent_trade_logs-trade_type', '3');
        $dbgrid->addValueFilter('account_agent_trade_logs-withdraw_type', array("1", "2", "3"), DBGridConstant::OPERATOR_IN);
        $dbgrid->addValueFilter('account_agent_trade_logs-status', 6);
        $dbgrid->addValueFilter('account_agent_trade_logs-is_delete', 0);
        $dbgrid->addFetchTable("account_agent_trade_logs", "*");
        $dbgrid->addFetchTable("users", array("name"),"users.id = account_agent_trade_logs.user_id and users.is_delete = 0");        
        $dbgrid->addFetchColumn("sn", $this->_translate->translate("SN"),"waitcheckonline");
        $dbgrid->addFetchColumn("name", $this->_translate->translate("userId"));
        $dbgrid->addFetchColumn("in_id", $this->_translate->translate("userAccount2"));
        $dbgrid->addFetchColumn("trade_sum", $this->_translate->translate("tradeSum"));
        $dbgrid->addFetchColumn("withdraw_type", $this->_translate->translate("withDrawType2"), "", TradeConstant::getTypeList());
        $dbgrid->addFetchColumn("created", $this->_translate->translate("withdrawApplyDate"));
        $dbgrid->setPageTitle($this->_translate->translate("waitCheckWithDrawOL"));
        $dbgrid->setModule('/trade/withdraw');
//        if(AAService::isAllowed("withdraw", "waitcheckonline")) {
//            $dbgrid->addColAction("waitcheckonline", $this->_translate->translate("lookInfor"), "transaction.gif");
//        }
//        if(AAService::isAllowed("withdraw", "deletelinewait")) {
//            $dbgrid->addColAction("deletelinewait", $this->_translate->translate("delBtn"), "delete.gif");
//        }
        $this->view->dbGrid = $dbgrid->render();
    }

    //waitcheckwithdrawonline log
    public function waitcheckonlineAction(){
        $id = $this->_getParam('id');
        $withDrawService = new WithDrawService();
        $withDraw = $withDrawService->getWithDrawInfoById($id);
        if(!empty($withDraw)){
            $agentTotal = $withDrawService->getAgentTotalByUserId($withDraw['user_id']);
        }
        $this->view->withdraw = $withDraw;
        $this->view->agentTotal = $agentTotal;
    }

    public function searchofflineAction(){
        $dbgrid = new DBGrid($this->view);
        $dbgrid->setPost($_POST);
        $dbgrid->setSortOrder("account_agent_trade_logs-created");
        //$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-sn", $this->_translate->translate("SN"));
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-trade_num", $this->_translate->translate("tradeNum"));
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "users-name", $this->_translate->translate("userId"));
        $dbgrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-status", $this->_translate->translate("status"),TradeConstant::getWithdrawStatusList());
        $dbgrid->addDateFilter("account_agent_trade_logs-created", $this->_translate->translate("withdrawApplyDate"));
        $dbgrid->addValueFilter('account_agent_trade_logs-trade_type', '3');
        $dbgrid->addValueFilter('account_agent_trade_logs-withdraw_type', array("11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24","25"), DBGridConstant::OPERATOR_IN);
        $dbgrid->addValueFilter('account_agent_trade_logs-status', array("4", "5","7"), DBGridConstant::OPERATOR_IN);
        $dbgrid->addValueFilter('account_agent_trade_logs-is_delete', 0);
        $dbgrid->addFetchTable("account_agent_trade_logs", "*");
        $dbgrid->addFetchTable("users", array("name"),"users.id = account_agent_trade_logs.user_id and users.is_delete = 0");
        //$dbgrid->addFetchColumn("sn", $this->_translate->translate("SN"));
        $dbgrid->addFetchColumn("trade_num", $this->_translate->translate("tradeNum"),"offline");
        $dbgrid->addFetchColumn("name", $this->_translate->translate("userId"));
        $dbgrid->addFetchColumn("in_id", $this->_translate->translate("userAccount2"));
        $dbgrid->addFetchColumn("in_name", $this->_translate->translate("returnAccountName2"));
        $dbgrid->addFetchColumn("trade_sum", $this->_translate->translate("tradeSum"));
        $dbgrid->addFetchColumn("withdraw_type", $this->_translate->translate("returnBank2"), "", TradeConstant::getTypeList());
        $dbgrid->addFetchColumn("status", $this->_translate->translate("status2"), "", TradeConstant::getStatusList());
        $dbgrid->addFetchColumn("created", $this->_translate->translate("withdrawApplyDate"));
        $dbgrid->setPageTitle($this->_translate->translate("withDrawOffLine"));
        $dbgrid->setModule('/trade/withdraw');
//        if(AAService::isAllowed("withdraw", "offline")) {
//            $dbgrid->addColAction("offline", $this->_translate->translate("lookInfor"), "transaction.gif");
//        }
//        if(AAService::isAllowed("withdraw", "deleteoffline")) {
//            $dbgrid->addColAction("deleteoffline", $this->_translate->translate("delBtn"), "delete.gif");
//        }        
        $this->view->dbGrid = $dbgrid->render();
    }

    //withdrawoffline log
    public function offlineAction(){
        $id = $this->_getParam('id');
        $withDrawService = new WithDrawService();
        $withDraw = $withDrawService->getWithDrawInfoById($id);
        $this->view->id = $id;
        if($withDraw['status'] == 4){
            $form = new SureWithDrawOffLineForm();
            $this->view->form=$form;
            $this->view->withdraw=$withDraw;
            return $this->render('offlinewaitin');      //go to offlinewaitin.phtml
        }
        if($withDraw['status'] == 5){
            $this->view->withdraw=$withDraw;
            return $this->render('offlinealreadyin');   //go to offlinealreadyin.phtml
        }
        if($withDraw['status'] == 7){
            $this->view->withdraw=$withDraw;
            return $this->render('offlinereject');      //go to offlinereject.phtml
        }
    }

    public function searchofflinewaitAction(){
        $dbgrid = new DBGrid($this->view);
        $dbgrid->setPost($_POST);
        $dbgrid->setSortOrder("account_agent_trade_logs-created");
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-sn", $this->_translate->translate("SN"));
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "users-name", $this->_translate->translate("userId"));
        $dbgrid->addDateFilter("account_agent_trade_logs-created", $this->_translate->translate("withdrawApplyDate"));
        $dbgrid->addValueFilter('account_agent_trade_logs-trade_type', '3');
        $dbgrid->addValueFilter('account_agent_trade_logs-withdraw_type', array("11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24","25"), DBGridConstant::OPERATOR_IN);
        $dbgrid->addValueFilter('account_agent_trade_logs-status', 6);
        $dbgrid->addValueFilter('account_agent_trade_logs-is_delete', 0);
        $dbgrid->addFetchTable("account_agent_trade_logs", "*");
        $dbgrid->addFetchTable("users", array("name"),"users.id = account_agent_trade_logs.user_id and users.is_delete = 0");
        $dbgrid->addFetchColumn("sn", $this->_translate->translate("SN"),"waitcheckoffline");
        $dbgrid->addFetchColumn("name", $this->_translate->translate("userId"));
        $dbgrid->addFetchColumn("in_id", $this->_translate->translate("userAccount2"));
        $dbgrid->addFetchColumn("in_name", $this->_translate->translate("returnAccountName2"));
        $dbgrid->addFetchColumn("trade_sum", $this->_translate->translate("tradeSum"));
        $dbgrid->addFetchColumn("withdraw_type", $this->_translate->translate("returnBank2"), "", TradeConstant::getTypeList());
        $dbgrid->addFetchColumn("created", $this->_translate->translate("withdrawApplyDate"));
        $dbgrid->setPageTitle($this->_translate->translate("waitCheckWithDrawOffL"));
        $dbgrid->setModule('/trade/withdraw');
//        if(AAService::isAllowed("withdraw", "waitcheckoffline")) {
//            $dbgrid->addColAction("waitcheckoffline", $this->_translate->translate("lookInfor"), "transaction.gif");
//        }
//        if(AAService::isAllowed("withdraw", "deleteofflinewait")) {
//            $dbgrid->addColAction("deleteofflinewait", $this->_translate->translate("delBtn"), "delete.gif");
//        }
        if(AAService::isAllowed("withdraw", "add")) {
                $dbgrid->addModuleAction("add",$this->_translate->translate("addBtn"),"add");
        }
        $this->view->dbGrid = $dbgrid->render();
    }

    //waitcheckwithdrawoffline log
    public function waitcheckofflineAction(){
        $id = $this->_getParam('id');
        $withDrawService = new WithDrawService();
//        $withDraw = $withDrawService->getAgentWithDrawInfoById($id);
        $withDrawService = new WithDrawService();
        $withDraw = $withDrawService->getWithDrawInfoById($id);
        if(!empty($withDraw)){
            $agentTotal = $withDrawService->getAgentTotalByUserId($withDraw['user_id']);
        }
        $this->view->withdraw = $withDraw;
        $this->view->agentTotal = $agentTotal;
    }

    //overpass auditing_online
    public function overpassonlineAction(){
        $onLine = 1;
        $loginUser = BaseUtil::getLoginUser();
        $tradeLogId = $this->_getParam('id');
        $withDrawService = new WithDrawService();
        $withDrawService->overPassCheckById($onLine,$loginUser,$tradeLogId);
        $this->_redirect("trade/withdraw/searchlinewait");
    }

    //reject withdraw online
    public function rejectonlineAction(){
        $loginUser = BaseUtil::getLoginUser();
        $withDrawService = new WithDrawService();
        $withDrawService->rejectById($_POST,$loginUser);
        $this->_redirector->gotoUrl('trade/withdraw/searchlinewait');
    }

    //sure withdraw online
    public function surewithdrawAction(){
        $loginUser = BaseUtil::getLoginUser();
        $tradeLogId = HttpUtil::getPostValue('tradeLogId');
        $form = new SureWithDrawLineForm();
        $withDrawService = new WithDrawService();
        if(!$form->isValid($_POST)){
            $withDraw = $withDrawService->getWithDrawInfoById($tradeLogId);
            $this->view->withdraw=$withDraw;
            $this->view->form=$form;
            $this->view->id = $tradeLogId;
            return $this->render('online');
        }
        $data = array();
        $data['trade_num'] = $form->getValue("trade_num");
        $data['out_id']=$form->getValue('out_id');
        $data['in_date']=$form->getValue('pay_time') ." " . $form->getValue('pay_h') . ":" . $form->getValue('pay_m') . ":00";
        $withDrawService->sureWithDraw($loginUser,$tradeLogId,$data);
        $this->_redirector->gotoUrl('trade/withdraw/searchline');
    }

    //overpass auditing_offline
    public function overpassofflineAction(){
        $offLine = 2;
        $loginUser = BaseUtil::getLoginUser();
        $tradeLogId = $this->_getParam('id');
        $withDrawService = new WithDrawService();
        $withDrawService->overPassCheckById($offLine,$loginUser,$tradeLogId);
        $this->_redirect("trade/withdraw/searchofflinewait");
    }

    //suer withdraw offline
    public function surewithdrawofflineAction(){
        $loginUser = BaseUtil::getLoginUser();
        $withDrawService = new WithDrawService();
        $form = new SureWithDrawOffLineForm();
        $id = HttpUtil::getPostValue('tradeLogId');
        $withDraw = $withDrawService->getWithDrawInfoById($id);
        if(!$form->isValid($_POST)){            
            $this->view->withdraw=$withDraw;
            $this->view->form=$form;
            $this->view->id = $id;
            return $this->render('offlinewaitin');
        }
        $data=array();
        $data['trade_num'] = $form->getValue('trade_num');
        $data['out_id']=$form->getValue('out_id');
        $data['in_date']=$form->getValue('pay_time') ." " . $form->getValue('pay_h') . ":" . $form->getValue('pay_m') . ":00";
        $withDrawService->sureWithDraw($loginUser,$id,$data,2);
        $this->_redirect("trade/withdraw/searchoffline");
    }

    //reject withdraw online
    public function rejectofflineAction(){
        $loginUser = BaseUtil::getLoginUser();
        $withDrawService = new WithDrawService();
        $withDrawService->rejectById($_POST,$loginUser,2);
        $this->_redirector->gotoUrl('trade/withdraw/searchofflinewait');
    }

    //add withdraw offline
    public function addAction(){
        $form = new OffLineAddWithDrawForm();
        $this->view->form=$form;
    }

    //addofflinewithdraw submit action
    public function subaddAction(){
        $loginUser = BaseUtil::getLoginUser();
        $withDrawService = new WithDrawService();
        $form = new OffLineAddWithDrawForm();
        if(!$form->isValid($_POST)){
            $this->view->form=$form;
            return $this->render('add');
        }
        $data=array();
        $data['account']=$form->getValue('account');
        $data['in_bank']=$form->getValue('in_bank');
        $data['in_bank_name']=$form->getValue('in_bank_name');
        $data['in_name']=$form->getValue('in_name');
        $data['in_id']=$form->getValue('in_id');
        $data['trade_sum']=$form->getValue('trade_sum');
        $withDrawService->offLineApplication($loginUser,$data);
        $this->_redirector->gotoUrl('trade/withdraw/searchofflinewait');
    }

//    public function deleteAction(){
//        $loginUser = BaseUtil::getLoginUser();
//        $tradeLogId = $this->_getParam('id');
//        $fromWhere = $this->_getParam('from');
//        $withDrawService = new WithDrawService();
//        $withDrawService->deleteTradeLogById($fromWhere,$tradeLogId);
////        $this->_redirector->gotoUrl('/');
//    }

//      public function deletelineAction(){
//          $loginUser = BaseUtil::getLoginUser();
//          $tradeLogId = $this->_getParam('id');
//          $withDrawService = new WithDrawService();
//          $withDrawService->deleteTradeLog($tradeLogId,$loginUser,"OLWDD");
//          $this->_redirect("trade/withdraw/searchline");
//      }
//
//      public function deletelinewaitAction(){
//          $loginUser = BaseUtil::getLoginUser();
//          $tradeLogId = $this->_getParam('id');
//          $withDrawService = new WithDrawService();
//          $withDrawService->deleteTradeLog($tradeLogId,$loginUser,"OLWDAD");
//          $this->_redirect("trade/withdraw/searchlinewait");
//      }
//
//      public function deleteofflineAction(){
//          $loginUser = BaseUtil::getLoginUser();
//          $tradeLogId = $this->_getParam('id');
//          $withDrawService = new WithDrawService();
//          $withDrawService->deleteTradeLog($tradeLogId,$loginUser,"OFWDD");
//          $this->_redirect("trade/withdraw/searchoffline");
//      }
//
//      public function deleteofflinewaitAction(){
//          $loginUser = BaseUtil::getLoginUser();
//          $tradeLogId = $this->_getParam('id');
//          $withDrawService = new WithDrawService();
//          $withDrawService->deleteTradeLog($tradeLogId,$loginUser,"OFWDAD");
//          $this->_redirect("trade/withdraw/searchofflinewait");
//      }
}

?>
